<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;



use app\admin\model\PayorderModel;

class Payorder extends Base
{
    //用户列表
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            $where[] = ['status', '=', 1];
            if (!empty($param['searchText'])) {
                $where[] = ['trade_no', 'like', '%' . $param['searchText'] . '%'];
            }
            if (!empty($param['searchType'])) {
                $where[] = ['trade_type', '=', $param['searchType']];
            }
            if (!empty($param['searchTime'])) {
                $where[] = ['create_time', 'between time', explode(' - ', $param['searchTime'])];
            }
            $user = new PayorderModel();
            $selectResult = $user->getUsersByWhere($where, $offset, $limit);

            // 拼装参数
            $price = 0;
            foreach($selectResult as $key=>$vo){

                $selectResult[$key]['type'] = $vo->type;
                $selectResult[$key]['user'] = $vo->memberModel()->value('memb_phone');
                $price += $vo->price;
                // $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            $return['price'] = $price;
            $return['total'] = $user->getAllUsers($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }

        return $this->fetch();
    }

    // 编辑用户
    public function edit()
    {
        $user = new OrderModel();

        if(request()->isPost()){

            $param = input('post.');
            
            $flag = $user->editUser($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');

        return $this->fetch('edit', ['order'=>OrderModel::find($id)]);
    }

    // 删除用户
    public function del()
    {
        $id = input('param.id');

        $role = new OrderModel();
        $flag = $role->del($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'order/edit',
                'href' => url('order/edit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'order/del',
                'href' => "javascript:dataDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }
}
